package com.example.controlecarro.DAO;

import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.controlecarro.R.id;
import com.example.controlecarro.R.layout;
import com.example.controlecarro.model.Carro;


public class CarroAdapter extends BaseAdapter {

	 private Context context;
	 
	    private List<Carro> listaCarro;
	    private LayoutInflater inflater;
	 
	    public CarroAdapter(Context context, List<Carro> listCar) {
	        this.context = context;
	        this.listaCarro = listCar;
	        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    }
	     
	    
	    @Override
	    public void notifyDataSetChanged() {   
	        try{
	            super.notifyDataSetChanged();
	        }catch (Exception e) {
	            trace("Erro : " + e.getMessage());
	        }
	    } 
	         
	    public int getCount() {
	        return listaCarro.size();
	    }
	 

	    public void remove(final Carro item) {
	        this.listaCarro.remove(item);
	    } 
	     

	    public void add(final Carro item) {
	        this.listaCarro.add(item);
	    }    
	     
	    public Object getItem(int position) {
	        return listaCarro.get(position);
	    }
	 
	    public long getItemId(int position) {
	        return position;
	    }
	 
	    public View getView(int position, View convertView, ViewGroup viewGroup) {
	        try
	        {
	             
	            Carro contato = listaCarro.get(position);
	 

	            ViewHolder holder;
	             
	       
	            if (convertView == null) {
	                convertView = inflater.inflate(layout.lista_carro, null);
	                 

	                holder = new ViewHolder();
	                holder.tvVeiculo= (TextView) convertView.findViewById(id.txtVeiculoL);
	                holder.tvPlaca = (TextView) convertView.findViewById(id.txtPlacal);
	                holder.tvAno = (TextView) convertView.findViewById(id.txtAnoL);
	                 
	                convertView.setTag(holder);
	            } else {
	           
	                holder = (ViewHolder) convertView.getTag();
	            }
	 
	            holder.tvVeiculo.setText(contato.getVeiculo());
	            holder.tvPlaca.setText(contato.getPlaca());
	            holder.tvAno.setText(contato.getAno());
	 
	            return convertView;            
	             
	        }catch (Exception e) {
	            trace("Erro : " + e.getMessage());
	        }
	        return convertView;
	    }
	 
	 
	    public void toast (String msg)
	    {
	        Toast.makeText (context, msg, Toast.LENGTH_SHORT).show ();
	    } 
	     
	    private void trace (String msg) 
	    {
	        toast (msg);
	    } 
	     
	    static class ViewHolder {
	        public TextView tvVeiculo;
	        public TextView tvPlaca;
	        public TextView tvAno;
	    }    



}
